﻿
var actionFlag = getPar1("actionFlag");    //1: 新增; 2: 修改
var const_UserId = parent.parent._currentUserID;
var const_UserName = parent.parent._currentUserName;


$(document).ready(function () {
    
    var now = new Date();

    if (actionFlag == "1") {
        obj = $("#BOMNo");
        genNewSequenceNo(obj, "BOMNo");
        $("#AddTime").val(fnGetMyDate(now));
        $("#AddUserName").val(const_UserName);
        $("#ReferenceNo").focus();

        $("#ItemNo").bind("change", function () { var itemNo = $(this).val(); setItemMaster(itemNo); });
        $("#ReferenceNo").bind("change", function () { var referenceNo = $(this).val(); fnCheckReferenceNo(referenceNo); });
    }

    if (actionFlag == "2") {
        $("#UpdateTime").val(fnGetMyDate(now));
    }

    $("#AddUserName").attr("disabled", "disabled");
    $("#AddTime").attr("disabled", "disabled");
    $("#UpdateUserName").attr("disabled", "disabled");
    $("#UpdateTime").attr("disabled", "disabled");
    $("#ItemName").attr("disabled", "disabled");
    $("#ItemModel").attr("disabled", "disabled");
});

/*
var fnGetMyDate = function (date) {
    y = date.getFullYear();
    m = date.getMonth() + 1;
    d = date.getDate();
    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;
    return myDate = y + "-" + m + "-" + d
}
*/

// 检测BOM管理编号是否重复
var fnCheckReferenceNo = function (val) {
    if (val != "") {
        var referenceNo = fnGetData("BH_BOM", "ID<>'" + instance[0].ID.toString() + "' and BOMNo='" + val + "'");
        if (referenceNo != null && referenceNo != "") {
            alert("管理编号: [ " + val + " ] 已存在, 请重新输入!");
            setTimeout(function () {
                $("#ReferenceNo").focus();
                $("#ReferenceNo").val("");
            }, 10);
        }
    }
}

var fnCustomeRowAddEvent = function (trs, i) {
    _DataTable.Rows[i].ID = "00000000-0000-0000-0000-000000000000";
    setLineNo();
    //debugger;
    var qtyObj = trs.find("input[name='Qty']");
    qtyObj.numberbox({
        min: 0,
        precision: 4
    });
    qtyObj.val("0.0000");

    var scrapObj = trs.find("input[name='Scrap']");
    scrapObj.numberbox({
        min: 0,
        precision: 4
    });
    scrapObj.val("0.0000");

    var o = trs.find("input[name='ChildItemNo']");
    o.bind("change", function () { var t = $(this).val(); var o = trs; setItemDetail(t, o); });
    o.focus();
    o.click();
}

var fnCustomeRowDelEvent = function () {
    setLineNo();
}

var customSetEvent = function () {
    if (actionFlag == "2") {
        var now = new Date();
        $("#UpdateTime").val(fnGetMyDate(now));
        $("#UpdateUserName").val(const_UserName);
        $("#ReferenceNo").attr("disabled", "disabled");
        $("#ItemNo").attr("disabled", "disabled");
        parent.setAddNewButtonHiden();
        //debugger;
        var trs = $("#Details tbody tr");
        if (trs.length >= 1) {
            trs.each(function (em) {
                $(trs[em]).find("input[name='ChildItemNo']").attr("disabled", "disabled");
                $(trs[em]).find("input[name='Qty']").attr("disabled", "disabled");
                $(trs[em]).find("input[name='Scrap']").attr("disabled", "disabled");
                $(trs[em]).find("input[name='Remark']").attr("disabled", "disabled");
            });
        }
        //debugger;
        if ($("#BeginDate").val() == "1900-01-01 ") {
            $("#BeginDate").val("");
        }
        if ($("#EndDate").val() == "1900-01-01 ") {
            $("#EndDate").val("");
        }
    }
}

var setLineNo = function () {
    var trs = $("#Details tbody tr");
    if (trs.length >= 1) {
        var seq = 1;
        trs.each(function (em) {
            $(trs[em]).find("input[name='LineNo']").val(seq.toString());
            seq = seq + 1;
        });
    }
}

var getItemData = function (item) {
    return fnGetData("BH_ItemMaster", "ItemNo='" + item + "' or ItemModel='" + item + "' or ItemName='" + item + "'");
}

var setItemMaster = function () {
    //debugger;
    var itemNo = $("#ItemNo").attr("SValue");
    ItemMaster = getItemData(itemNo);

    if (ItemMaster == null || ItemMaster == "") {
        alert("物料：" + itemNo + " 还未定义!");
        //$("#ItemNo").val("");
        $("#ItemModel").val("");
        $("#ItemName").val("");
        $("#ItemNo").focus();
        return;
    }

    //$("#ItemNo").val(ItemMaster.rows[0].ItemNo);
    $("#ItemModel").val(ItemMaster.rows[0].ItemModel);
    $("#ItemName").val(ItemMaster.rows[0].ItemName);
}

var setItemDetail = function (itemNo, o) {
    if (itemNo != "" && itemNo != null) {
        ItemDetail = getItemData(itemNo);

        if (ItemDetail == null || ItemDetail == "") {
            alert("物料：" + itemNo + " 还未定义!");
            o.find("input[name='ChildItemNo']").val("");
            o.find("input[name='ItemModel']").val("");
            o.find("input[name='ItemName']").val("");
            setTimeout(function () {
                o.find("input[name='ChildItemNo']").focus();
            }, 10);
            return;
        }

        o.find("input[name='ChildItemNo']").val(ItemDetail.rows[0].ItemNo);
        o.find("input[name='ItemModel']").val(ItemDetail.rows[0].ItemModel);
        o.find("input[name='ItemName']").val(ItemDetail.rows[0].ItemName);
    }
} 


// 数据检测
var fnCustomCheck = function () {
    if ($("#ReferenceNo").val() == "" || $("#ReferenceNo").val() == null) {
        $("#ReferenceNo").focus();
        return "管理编号不能为空!";
    }

    if ($("#BeginDate").val() == "" || $("#BeginDate").val() == null) {
        $("#BeginDate").focus();
        return "开始使用日期不能为空!";
    }

    if ($("#EndDate").val() != "" && $("#EndDate").val() != null) {
        if ($("#EndDate").val() < $("#BeginDate").val()) {
            $("#EndDate").focus();
            return "结束使用日期不于小于开始使用日期!";
        }
    }

    if ($("#ItemNo").attr("SValue") == "" || $("#ItemNo").attr("SValue") == null) {
        $("#ItemNo").focus();
        return "半成品或成品不能为空!"
    }

    return "";
}

// 检测明细表中的记录是否录入
var fnCheckDetail = function () {
    var trs = $("#Details tbody tr");
    var errorFlag = 0;

    if (trs.length < 1) {
        alert("物料列表中不存在任何部品或半成品!");
        return false;
    }

    trs.each(function (em) {
        var itemNo = $(trs[em]).find("input[name='ChildItemNo']");
        var qty = $(trs[em]).find("input[name='Qty']");
        var scrap = $(trs[em]).find("input[name='Scrap']");

        if (itemNo.val() == "") {
            alert("物料列表中的部品或半成品编号不能为空!");
            itemNo.focus();
            errorFlag = 1
            return false;
        }

        if (parseFloat(qty.val().toString()) == 0) {
            alert("物料列表中的部品或半成品使用量不能为0!");
            qty.focus();
            errorFlag = 1
            return false;
        }
    });

    if (errorFlag == 1) {
        return false;
    }

    return true;
}

var fnSetValue = function () {
    instance[0].BOMNo = instance[0].BOMNo.toUpperCase();
    instance[0].ReferenceNo = instance[0].ReferenceNo.toUpperCase();
    now = new Date();
    instance[0].UpdateTime = fnGetMyDate(now);
    if (actionFlag == "1") {
        instance[0].AddUser = const_UserId;
    }
    if (actionFlag == "2") {
        instance[0].UpdateUser = const_UserId;
    }
}

// 保存数据
var fnCustomSave = function () {
    //debugger;
    if (fnCheckDetail() == false) {
        return;
    }

    fnSetValue();

    dataAction = (actionFlag == "1") ? "A" : "C";
    //debugger;
    if (typeof (_DataTable) != "undefined") {
        if (fnCheckEmpty() == false) {
            alert("明细数据没有录入");
        }
        _DataTable.GetData();

        detailJson = $.toJSON(_DataTable.Rows).replace(/'/g, "[B]").replace(/\+/g, "[J]")
    }

    json = $.toJSON(instance[0]).replace(/'/g, "[B]").replace(/\+/g, "[J]");
    //debugger;
    $.ajax({
        async: false, //异步请求
        cache: false, //不从浏览器缓存中加载
        type: "GET",
        /*dataType: "json",*/
        url: "/DCBH/DCBHCommonOperator.aspx",
        data: { Action: "procedure", entity: "V_BH_BOM", json: json, detailJson: detailJson, dataAction: dataAction },
        success: function (data) {
            //debugger;
            if (data != "") {
                if (data == "1") {
                    alert("数据保存成功!");

                    if (parent.SaveAndAddNewFlag == true) {
                        location.href = location.href
                    }

                    if (parent.SaveSuccessClose == true) {
                        parent.parent.CloseCurPage();
                    }
                }
                else {
                    alert(data);
                }
            }
        },
        error: function (e) {
            //debugger;
            alert("保存失败, 请联系开发人员，核查系统！");
            return;
        }
    });

}